from flask import Blueprint, request, jsonify

from utils.db import db1
from .compan import get_data
from .compan import xtree
import time

bp_zuoye = Blueprint('zuoye', __name__)


@bp_zuoye.route('/login', methods=['POST', 'GET'])
def login():
    if request.method == 'POST':
        data = get_data()
        sql1 = """select * from zusers where username = '%s'""" % (data['username'])
        res1 = db1.findone(sql1)
        if res1:
            if int(data['password']) == int(res1['password']):

                return jsonify({'code': 200, 'userid': res1['id'], 'name': res1['name']})
            else:
                return jsonify({"code": 20010, 'mes': '密码错误'})
        else:
            return jsonify({'code': 20011, 'mes': '用户名错误'})
    elif request.method == 'GET':
        userid = int(request.args.get('userid'))
        sql1 = """select z.* from juese_ziyuan as j left join ziyuan as z on j.ziyuanid = z.id where j.userid = %d""" % (
            userid)
        res1 = db1.findall(sql1)

        list = xtree(res1)
        return jsonify({'code': 200, 'list': list})


@bp_zuoye.route('/xiaoshuo', methods=['POST', 'GET'])
def xiaoshuo():
    if request.method == 'POST':
        data = get_data()
        sql = 'insert into xiaoshuo(name,userid) values("%s",%d)' % (
            data['name'], int(data['userid']))
        db1.update(sql)
        return jsonify({'code': 200})
    elif request.method == 'GET':
        userid = int(request.args.get('userid'))
        sql = 'select * from xiaoshuo where userid = %d' % (userid)
        res = db1.findall(sql)
        return jsonify({'code': 200, 'list': res})


@bp_zuoye.route('/shenhe', methods=['POST', 'GET'])
def shenhe():
    if request.method == 'GET':
        sql = 'select x.*,z.name as uname from xiaoshuo as x left join zusers as z on x.userid = z.id where x.status = 1'
        res = db1.findall(sql)
        return jsonify({'code': 200, 'list': res})
    elif request.method == 'POST':
        data = get_data()
        if int(data['type']) == 1:
            sql1 = '''select * from xiaoshuo where id = %d''' % (int(data['id']))
            res1 = db1.findone(sql1)
            sql2 = '''update xiaoshuo set status = 2 where id = %d''' % (int(data['id']))
            db1.update(sql2)
            sql3 = '''select jifen from zusers where id = %d''' % (int(res1['userid']))
            jf = db1.findone(sql3)
            jifen = int(jf['jifen']) + 2
            sql4 = '''update zusers set jifen = %d where id = %d''' % (jifen, int(res1['userid']))
            db1.update(sql4)
        else:
            sql2 = '''update xiaoshuo set status = 3 where id = %d''' % (int(data['id']))
            db1.update(sql2)

        return jsonify({'code': 200})


@bp_zuoye.route('/lipin', methods=['POST', 'GET', 'PUT'])
def lipin():
    if request.method == 'POST':
        data = get_data()
        sql = '''insert into lipin(name,money) values("%s",%d)''' % (data['name'], int(data['money']))
        db1.update(sql)
        return jsonify({'code': 200})
    elif request.method == 'GET':
        sql = 'select * from lipin'
        res = db1.findall(sql)
        return jsonify({'code': 200, 'list': res})
    elif request.method == 'PUT':
        data = get_data()
        sql1 = '''select jifen from zusers where id = %d''' % (int(data['userid']))
        res1 = db1.findone(sql1)
        sql2 = '''select money from lipin where id = %d''' % (int(data['id']))
        res2 = db1.findone(sql2)
        if int(res2['money']) <= int(res1['jifen']):
            jifen = int(res1['jifen']) - int(res2['money'])
            sql3 = '''update zusers set jifen = %d where id = %d''' % (jifen, int(data['userid']))
            sql4 = '''insert into mylipin(userid,lipinid) values(%d,%d)''' % (int(data['userid']), int(data['id']))
            try:
                db1.update(sql4)
                db1.update(sql3)
                return jsonify({'code': 200})
            except:
                return jsonify({'code': 20013, 'mes': '兑换失败'})
        else:
            return jsonify({'code': 20014, 'mes': '积分不足'})


@bp_zuoye.route('/jifen', methods=['GET', 'POST'])
def jifen():
    if request.method == 'GET':
        userid = int(request.args.get('userid'))
        sql = 'select jifen from zusers where id = %d' % (userid)
        res = db1.findone(sql)
        jifen = res['jifen']
        return jsonify({'code': 200, 'jifen': jifen})
    elif request.method == 'POST':
        sql = '''select * from zusers'''
        res = db1.findall(sql)
        list = []
        for i in res:
            dict = {'value': i['jifen'], 'name': i['name']}
            list.append(dict)
        return jsonify({'code': 200, 'list': list})
